# -*- coding:utf8 -*-
import csv
import Image
import os
import random

def abandon_samples(c):#0 #4 #11 #13 #14 #15 #20 #22 #23 #25 #26 #27 #28 #29 #31 #32 #33 #35 #37 #38
    category = c
    print "label: " + str(category)
    csv_name = 'label' + str(category) + '.csv'
    res_csv = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/smallDataSet/one/train224/csvFile/' + csv_name
    txtFile = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/washed_train_label.csv'
    f = open(txtFile)
    f_csv = file(res_csv,'wb')
    csvwriter = csv.writer(f_csv)

    lines = f.readlines()
    flag = 0
    for line in lines:
        line = line.strip('\n')
        name, label = line.split(',')
        label = label.strip('\r')
        if int(label) == category:
            new_line = [str(name), str(label)]
            csvwriter.writerow(new_line)
            flag += 1
            if flag%1000 == 0:
                print flag,
        if flag >=10000:
            break
    f.close()
    f_csv.close()

def flip_samples():
    category = 36
    print "label: " + str(category)
    img_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/train'
    csv_name = 'label' + str(category) + '.csv'
    res_csv = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/smallDataSet/one/train224/csvFile/' + csv_name
    txtFile = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/washed_train_label.csv'
    f = open(txtFile)
    f_csv = file(res_csv,'wb')
    csvwriter = csv.writer(f_csv)

    lines = f.readlines()
    flag = 0
    times = 0
    for line in lines:
        line = line.strip('\n')
        times += 1
        name, label = line.split(',')
        label = label.strip('\r')
        if int(label) == category:
            flag += 2

            path = img_folder + '/' + name
            path_save = img_folder + '/' + "flip_" + name
            im = Image.open(path)
            im_flip = im.transpose(Image.FLIP_LEFT_RIGHT)
            im_flip.save(path_save)

            new_line1 = [name, str(label)]
            new_line2 = ['flip_'+name, str(label)]
            csvwriter.writerow(new_line1)
            csvwriter.writerow(new_line2)

            if flag%1000 == 0:
                print flag
        if times%1000 == 0:
            print str(times) + '\r',
    f.close()
    f_csv.close()

def crop4_samples(c):#2#5#7
    category = c
    print 'label: ' + str(category)
    img_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/train'
    csv_name = 'label' + str(category) + '.csv'
    res_csv = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/smallDataSet/one/train224/csvFile/' + csv_name
    txtFile = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/washed_train_label.csv'
    f = open(txtFile)
    f_csv = file(res_csv,'wb')
    csvwriter = csv.writer(f_csv)

    lines = f.readlines()
    flag = 0
    times = 0
    box = []
    box.append((1,1,225,225))
    box.append((31,1,255,225))
    box.append((1,31,225,255))
    box.append((31,31,255,255))
    for line in lines:
        line = line.strip('\n')
        times += 1
        name, label = line.split(',')
        label = label.strip('\r')
        if int(label) == category:
            flag += 4
            path = img_folder + '/' + name
            im = Image.open(path)
            for i in range(4):
                j = i + 1
                path_save = img_folder + '/' + 'crop4_' + str(j) + name
                im_save = im.crop(box[i])
                im_save.save(path_save)
                new_line = ['crop4_' + str(j) + name, str(label)]
                csvwriter.writerow(new_line)
            if flag%1000 == 0:
                print 'flag: ' + str(flag)
        if times%1000 == 0:
            print 'times: ' + str(times) + '\r',
    f.close()
    f_csv.close()

def crop7_samples(c):#1#24#30
    category = c
    print 'label: ' + str(category)
    img_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/train'
    csv_name = 'label' + str(category) + '.csv'
    res_csv = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/smallDataSet/one/train224/csvFile/' + csv_name
    txtFile = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/washed_train_label.csv'
    f = open(txtFile)
    f_csv = file(res_csv,'wb')
    csvwriter = csv.writer(f_csv)

    lines = f.readlines()
    flag = 0
    times = 0
    box = []
    box.append((1,1,225,225))
    box.append((31,1,255,225))
    box.append((1,31,225,255))
    box.append((31,31,255,255))
    box.append((16,16,240,240))
    box.append((1,16,225,240))
    box.append((16,1,240,225))
    for line in lines:
        line = line.strip('\n')
        times += 1
        name, label = line.split(',')
        label = label.strip('\r')
        if int(label) == category:
            flag += 7
            path = img_folder + '/' + name
            im = Image.open(path)
            for i in range(7):
                j = i + 1
                path_save = img_folder + '/' + 'crop7_' + str(j) + name
                im_save = im.crop(box[i])
                im_save.save(path_save)
                new_line = ['crop7_' + str(j) + name, str(label)]
                csvwriter.writerow(new_line)
            if flag%1000 == 0:
                print 'flag: ' + str(flag)
        if times%1000 == 0:
            print 'times: ' + str(times) + '\r',
    f.close()
    f_csv.close()

def crop12_samples(c):#3 #9 #10 #12 #34 #39
    category = c
    print 'label: ' + str(category)
    img_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/train'
    csv_name = 'label' + str(category) + '.csv'
    res_csv = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/smallDataSet/one/train224/csvFile/' + csv_name
    txtFile = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/washed_train_label.csv'
    f = open(txtFile)
    f_csv = file(res_csv,'wb')
    csvwriter = csv.writer(f_csv)

    lines = f.readlines()
    flag = 0
    times = 0
    box = []
    box.append((1,1,225,225))
    box.append((31,1,255,225))
    box.append((1,31,225,255))
    box.append((31,31,255,255))
    box.append((16,16,240,240))
    box.append((1,16,225,240))
    box.append((16,1,240,225))
    box.append((1,23,225,247))
    box.append((23,1,247,225))
    box.append((23,16,247,240))
    box.append((16,23,240,247))
    box.append((13,13,237,237))
    for line in lines:
        line = line.strip('\n')
        times += 1
        name, label = line.split(',')
        label = label.strip('\r')
        if int(label) == category:
            flag += 12
            path = img_folder + '/' + name
            im = Image.open(path)
            for i in range(12):
                j = i + 1
                path_save = img_folder + '/' + 'crop12_' + str(j) + name
                im_save = im.crop(box[i])
                im_save.save(path_save)
                new_line = ['crop12_' + str(j) + name, str(label)]
                csvwriter.writerow(new_line)
            if flag%1200 == 0:
                print 'flag: ' + str(flag)
        if times%10000 == 0:
            print 'times: ' + str(times) + '\r',
    f.close()
    f_csv.close()

def crop20_flip_samples(c):#16 #17 #18 #19 #21
    category = c
    print 'label: ' + str(category)
    img_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/train'
    res_csv = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/smallDataSet/one/train224/csvFile/label21.csv'
    txtFile = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/washed_train_label.csv'
    f = open(txtFile)
    f_csv = file(res_csv,'wb')
    csvwriter = csv.writer(f_csv)

    lines = f.readlines()
    flag = 0
    times = 0
    box = []
    for k in range(20):
        x_ = int(random.random()*31)
        y_ = int(random.random()*31)
        box_ = (x_,y_,x_+224,y_+224)
        box.append(box_)
    for line in lines:
        line = line.strip('\n')
        times += 1
        name, label = line.split(',')
        label = label.strip('\r')
        if int(label) == category:
            flag += 40
            path = img_folder + '/' + name
            im = Image.open(path)
            for i in range(20):
                j = i + 1
                path_save_crop = img_folder + '/' + 'crop20_' + str(j) + name
                path_save_flip = img_folder + '/' + 'crop20_flip_' + str(j) + name
                im_save_crop = im.crop(box[i])
                im_save_flip = im.transpose(Image.FLIP_LEFT_RIGHT)
                im_save_crop.save(path_save_crop)
                im_save_flip.save(path_save_flip)
                new_line_crop = ['crop20_' + str(j) + name, str(label)]
                new_line_flip = ['crop20_flip_' + str(j) + name, str(label)]
                csvwriter.writerow(new_line_crop)
                csvwriter.writerow(new_line_flip)
            if flag%1000 == 0:
                print 'flag: ' + str(flag)
        if times%10000 == 0:
            print 'times: ' + str(times) + '\r',
    f.close()
    f_csv.close()

def calculate_categories():
    csv_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/smallDataSet/one/train224/csvFile'
    for file in os.listdir(csv_folder):
        path = csv_folder + '/' + file
        f = open(path,'rb')
        lines = f.readlines()
        print file + ': ' + str(len(lines))

if __name__ == "__main__":
    for i in [16, 17, 18, 19, 21]:
        crop20_flip_samples(c=i)